<template>
  <div>
    <el-dialog
      :visible="dialogVisible"
      :before-close="handleBeforeClose"
      :title="dialogTitle"
      width="480px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
        >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="handleBeforeClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="submitForm"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleListAPI } from '@/api/system'
import {
  createEmployeeAPI,
  editEmployeeAPI,
  getEmployeeDetailAPI
} from '@/api/employee'
export default {
  name: 'AddEmployee',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogTitle: {}
  },
  data () {
    return {
      addForm: {
        name: '',
        userName: '',
        roleId: '',
        roleName: '',
        status: '',
        phonenumber: ''
      },
      addFormRules: {
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [{ required: true, message: '请分配角色', trigger: 'blur' }],
        status: [{ required: true, message: '请选择员工状态', trigger: 'blur' }]
      },
      roleList: []
    }
  },
  methods: {
    async getEmployeeById (id) {
      const { data } = await getEmployeeDetailAPI(id)
      delete data.createTime
      this.addForm = data
    },
    submitForm () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return

        if (this.addForm.id) {
          await editEmployeeAPI(this.addForm)
          this.$message.success('编辑员工成功')
          delete this.addForm.id
          this.addForm.roleName = ''
        } else {
          await createEmployeeAPI(this.addForm)
          this.$message.success('添加员工成功')
        }

        this.$parent.getEmployeeList()
        this.$refs.addForm.resetFields()
        this.handleBeforeClose()
      })
    },
    async getRoleList () {
      const { data } = await getRoleListAPI()
      this.roleList = data
    },
    handleBeforeClose () {
      this.$emit('update:dialogVisible', false)
      this.$refs.addForm.resetFields()
    }
  },
  async created () {
    this.getRoleList()
  }
}
</script>

<style lang="scss" scoped></style>
